package com.bsb.biz.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Const;
import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.UmengPushUtil;
import com.bsb.base.model.BsbMsg;
import com.bsb.base.model.BsbMsgExample;
import com.bsb.base.model.BsbMsgExample.Criteria;
import com.bsb.base.model.User;
import com.bsb.biz.service.BsbMsgService;
import com.bsb.biz.shiro.UserUtils;

import push.AndroidNotification;
import push.PushClient;
import push.android.AndroidBroadcast;
import push.ios.IOSBroadcast;

@Controller
@RequestMapping("/bsbMsg")
public class BsbMsgController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbMsg/list.do";
	
	@Resource
	private BsbMsgService bsbMsgService;
	@Autowired
	private PushClient client;
	
	@RequiresPermissions("bsbMsg:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbMsgExample bsbMsgExample = new BsbMsgExample();
		Criteria criteria = bsbMsgExample.createCriteria();
		criteria.andDelFlagEqualTo(Global.SHOW);
		int totalCount = bsbMsgService.countBsbMsg(bsbMsgExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbMsgExample.setPageIndex((pageNum - 1) * pageSize);
		bsbMsgExample.setPageSize(pageSize);
		List<BsbMsg> bsbMsgs = bsbMsgService.getPageBsbMsg(bsbMsgExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbMsgs", bsbMsgs);
		return "/BsbMsg/list";
	}
	
	@RequiresPermissions("bsbMsg:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbMsg/add";
	}

	@RequiresPermissions("bsbMsg:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbMsg") BsbMsg bsbMsg) {
		bsbMsg.setInsertId(UserUtils.currentUser().getId().intValue());
		bsbMsgService.addBsbMsg(bsbMsg);
		return redirect;
	}
	
	@RequiresPermissions("bsbMsg:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String del(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) {
		String queryString = request.getQueryString();
		bsbMsgService.delBsbMsgById(id);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbMsg:hide")
	@RequestMapping(value = "/{id}/hide", method = RequestMethod.GET)
	public String hide(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) {
		String queryString = request.getQueryString();
		BsbMsg bsbMsg = bsbMsgService.getBsbMsgById(id);
		if(null != bsbMsg){
			bsbMsg.setDelFlag(Global.HIDE);
			bsbMsg.setUpdateId(UserUtils.currentUser().getId().intValue());
			bsbMsgService.updateBsbMsg(bsbMsg);
		}
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbMsg:publish")
	@RequestMapping(value = "/{id}/publish", method = RequestMethod.GET)
	public String publish(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) throws Exception {
		String queryString = request.getQueryString();
		BsbMsg bsbMsg = bsbMsgService.getBsbMsgById(id);
		String type = "1";
		if(null != bsbMsg){
			bsbMsg.setPublishStatus("1");
			bsbMsg.setPublishTime(new Date());
			bsbMsg.setUpdateId(UserUtils.currentUser().getId().intValue());
			bsbMsgService.updateBsbMsg(bsbMsg);
			
			String title = bsbMsg.getTitle();
			String msgid = bsbMsg.getId().toString();
			Map<String,String> m = new HashMap<String,String>();
			m.put("type", type );
			m.put("uuid", msgid);
			try {
				UmengPushUtil.sendAndroidBroadcast(title, m);
				UmengPushUtil.sendIOSBroadcast(title, m);
			} catch (Exception e) {
			}
			
		}
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbMsg:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toEdit(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) {
		BsbMsg bsbMsg = bsbMsgService.getBsbMsgById(id);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		model.addAttribute("bsbMsg", bsbMsg);
		return "/BsbMsg/edit";
	}

	@RequiresPermissions("bsbMsg:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model,@RequestParam String queryString,
			@ModelAttribute("bsbMsg") BsbMsg bsbMsg) {
		bsbMsg.setUpdateId(UserUtils.currentUser().getId().intValue());
		bsbMsgService.updateBsbMsg(bsbMsg);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbMsg:view")
	@RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("id") Integer id) {
		BsbMsg bsbMsg = bsbMsgService.getBsbMsgById(id);
		model.addAttribute("bsbMsg", bsbMsg);
		return "/BsbMsg/view";
	}
}
